package youleche.com.common;

import java.util.LinkedList;
import java.util.List;

import youleche.com.business.BBaseParamInfo;
import youleche.com.caches.BaseParamInfoCache;
import youleche.com.caches.CacheFactory;
import youleche.com.model.MBaseParamInfo;
import youleche.com.util.DateUtil;

/**
 * 菜单工厂
 * @author Administrator
 *
 */
public class MenuFactory {
	/**
	 * 获取头菜单
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getTopMenu(){
		List<MBaseParamInfo> baseParams = null;
		try {
			baseParams = (List<MBaseParamInfo>) BaseParamInfoCache.getCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(baseParams == null || baseParams.size() == 0){
			baseParams = new LinkedList<MBaseParamInfo>();
			MBaseParamInfo baseParam = new MBaseParamInfo();
			baseParam.setParamName("topMenu");
			baseParam.setParamValue("首页");
			baseParams.add(baseParam);
		}
	
		StringBuffer sb = new StringBuffer();
		sb.append("<ul>");
		sb.append("<li><a href=\"home.jsp\">首页</a></li>");
		for (MBaseParamInfo info : baseParams) {
			if("topMenu".equals(info.getParamName())){
				sb.append("<li>");
				sb.append(info.getParamValue());
				sb.append("</li>");
			}
		}
		
		sb.append("<li>更多>></li>");
		sb.append("</ul>");
		
		return sb.toString();
	}
	
	/**
	 * 获取管理员菜单
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getAdminMenu(){
		List<MBaseParamInfo> baseParams = null;
		try {
			baseParams = (List<MBaseParamInfo>) BaseParamInfoCache.getCache();
			if(baseParams == null || baseParams.size() == 0){
				initAdminMenu();
				baseParams = (List<MBaseParamInfo>) BaseParamInfoCache.getCache();
			}else{
				boolean isContains = false;
				for (MBaseParamInfo param : baseParams) {
					if(param.getParamName().contains("adminMenu")){
						isContains = true;
						break;
					}
				}
				
				if(!isContains){
					initAdminMenu();
					baseParams = (List<MBaseParamInfo>) BaseParamInfoCache.getCache();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer sb = new StringBuffer();
		sb.append("<ul class=\"sf-menu\">");
		for (MBaseParamInfo info : baseParams) {
			if("adminMenu".equals(info.getParamName()) && "000000".equals(info.getAssociatedKeyID())){
				sb.append("<li class=\"current\">");
				if("添加菜单".equals(info.getParamValue())){
					sb.append("<a href=\"addMenu.jsp\">");
				}else{
					sb.append("<a href=\"#\">");
				}

				sb.append(info.getParamValue());
				sb.append("</a>");	
				sb.append("<ul>");
				sb.append(getEditLi(info.getKeyID(), "1", true));
				if(hasSubMenu(info.getKeyID())){
					for(MBaseParamInfo subParam : baseParams){
						if(info.getKeyID().equals((subParam.getAssociatedKeyID()))){
							sb.append("<li>");
							sb.append("<a href=\""+subParam.getRemark()+"\">"+subParam.getParamValue()+"</a>");

							sb.append("<ul>");
							sb.append(getEditLi(subParam.getKeyID(), "2", true));
							if(hasSubMenu(subParam.getKeyID())){
								boolean isDelete = true;
								for(MBaseParamInfo secsubParam : baseParams){
									if(subParam.getKeyID().equals((secsubParam.getAssociatedKeyID()))){
										if(isDelete){
											sb.append(getEditLi(subParam.getKeyID(), "3", true));
											isDelete = false;
										}
										sb.append("<li>");
										sb.append("<a href=\""+subParam.getRemark()+"\">"+subParam.getParamValue()+"</a>");
										
										
										if(hasSubMenu(secsubParam.getKeyID())){
											for(MBaseParamInfo foursubParam : baseParams){
												if(secsubParam.getKeyID().equals((foursubParam.getAssociatedKeyID()))){
																									
													getEditLi(secsubParam.getKeyID(), "3", false);						
												}
											}
										}

										sb.append("</ul>");
										sb.append("</li>");							
									}
								}
								
							}
							
							sb.append("</ul>");
							sb.append("</li>");							
						}
					}
				}
				
				sb.append("</ul>");
				sb.append("</li>");
			}
		}
		
		sb.append("</ul>");

		
		return sb.toString();
	}
	
	/**
	 * 是否拥有子菜单
	 * @param keyId 菜单id
	 * @return 返回是否拥有子菜单 true-拥有
	 */
	@SuppressWarnings("unchecked")
	private static boolean hasSubMenu(String keyId){
		List<MBaseParamInfo> baseParams = null;
		try {
			baseParams = (List<MBaseParamInfo>) BaseParamInfoCache.getCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(baseParams != null && baseParams.size() > 0){
			for(MBaseParamInfo info : baseParams){
				if(keyId.equals(info.getAssociatedKeyID())){
					return true;
				}
			}
		}
		
		return false;
	}
	
	/**
	 * 初始化菜单
	 */
	@SuppressWarnings("static-access")
	private static void initAdminMenu(){
		MBaseParamInfo baseParam = new MBaseParamInfo();
		baseParam.setParamName("adminMenu");
		baseParam.setParamValue("添加菜单");
		baseParam.setAssociatedKeyID("000000");
		baseParam.setRemark("addMenu.jsp");
		baseParam.setAddTime(DateUtil.getNowDate());
		MBaseParamInfo baseParamType = new MBaseParamInfo();
		baseParamType.setParamName("类别");
		baseParamType.setParamValue("adminMenu");
		baseParamType.setAssociatedKeyID("000000");
		baseParamType.setRemark("管理员菜单");
		baseParamType.setAddTime(DateUtil.getNowDate());
		BBaseParamInfo handler = new BBaseParamInfo();
		try {
			handler.addBaseParamInfo(baseParamType);
			handler.addBaseParamInfo(baseParam);
			CacheFactory.getBaseParamInfoCache().updateCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取菜单类别
	 * @return 菜单类别
	 */
	@SuppressWarnings("unchecked")
	public static List<String> getMenuType(){
		List<MBaseParamInfo> baseParams = null;
		try {
			baseParams = (List<MBaseParamInfo>) BaseParamInfoCache.getCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List<String> types = null;
		if(baseParams != null && baseParams.size() > 0){
			types = new LinkedList<String>();
			for(MBaseParamInfo info : baseParams){
				if(info.getParamName().equals("类别") && !types.contains(info.getParamValue())){
					types.add(info.getParamValue() + "-" + info.getRemark());
				}
			}
		}
		
		return types;
	}
	
	/**
	 * 获取添加、删除、编辑li
	 * @param keyId id
	 * @param level 几级菜单
	 * @param isNeedAdd 是否需要add菜单
	 * @return 添加、删除、编辑li
	 */
	private static String getEditLi(String keyId, String level, boolean isNeedAdd){
		String addStr = "<img  onclick=\"addMenu('"+keyId+"', '"+level+"');\" src=\"../image/add_icon.jpg\" alt=\"添加子菜单\" />&nbsp;&nbsp;|&nbsp;&nbsp;";
		String editStr = "<li><a href=\"#\">";
		if(isNeedAdd){
			editStr += addStr;
		}

		editStr += "<img src=\"../image/edit.png\"  onclick=\"editMenu('"+keyId+"', '"+level+"');\" alt=\"编辑子菜单\"/>&nbsp;&nbsp;|&nbsp;&nbsp;<img src=\"../image/delete.jpg\" onclick=\"deleteMenu('"+keyId+"', '"+level+"');\"  alt=\"删除子菜单\"/></a></li>";
		
		return editStr;
	}
}
